/*
 * Copyright 2004,2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ws.commons.tcpmon;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.table.TableModel;

import org.apache.commons.httpclient.ChunkedInputStream;

import java.awt.Color;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * this class handles the pumping of data from the incoming socket to the
 * outgoing socket
 */
class Copy_2_of_SocketRR extends Thread {

	/**
	 * Field inSocket
	 */
	Socket inSocket = null;

	/**
	 * Field outSocket
	 */
	Socket outSocket = null;

	/**
	 * Field textArea
	 */
	JTextArea textArea;

	/**
	 * Field in
	 */
	InputStream in = null;

	/**
	 * Field out
	 */
	OutputStream out = null;

	/**
	 * Field xmlFormat
	 */
	boolean xmlFormat;

	/**
	 * Field done
	 */
	volatile boolean done = false;

	/**
	 * Field tmodel
	 */
	volatile long elapsed = 0;

	/**
	 * Field tmodel
	 */
	TableModel tmodel = null;

	/**
	 * Field tableIndex
	 */
	int tableIndex = 0;

	/**
	 * Field type
	 */
	String type = null;

	/**
	 * Field myConnection
	 */
	Connection myConnection = null;

	/**
	 * Field slowLink
	 */
	SlowLinkSimulator slowLink;

	private boolean canSuspend;

	private JCheckBox filterBox;

	private JButton submitButton;

	/**
	 * Constructor SocketRR
	 * 
	 * @param c
	 * @param inputSocket
	 * @param inputStream
	 * @param outputSocket
	 * @param outputStream
	 * @param _textArea
	 * @param format
	 * @param tModel
	 * @param index
	 * @param type
	 * @param slowLink
	 * @param b
	 * @param box
	 * @param submitButton
	 */
	public Copy_2_of_SocketRR(Connection c, Socket inputSocket, InputStream inputStream,
			Socket outputSocket, OutputStream outputStream,
			JTextArea _textArea, boolean format, TableModel tModel, int index,
			final String type, SlowLinkSimulator slowLink, boolean b,
			JCheckBox box, JButton submitButton) {
		inSocket = inputSocket;
		in = inputStream;
		outSocket = outputSocket;
		out = outputStream;
		textArea = _textArea;
		xmlFormat = format;
		tmodel = tModel;
		tableIndex = index;
		this.type = type;
		myConnection = c;
		this.slowLink = slowLink;
		this.canSuspend = b;
		this.filterBox = box;
		this.submitButton = submitButton;
		start();
	}

	/**
	 * Method isDone
	 * 
	 * @return boolean
	 */
	public boolean isDone() {
		return done;
	}

	public String getElapsed() {
		return String.valueOf(elapsed);
	}

	/**
	 * Method run
	 */
	public void run() {
		try {
			
			ChunkedInputStream b = new ChunkedInputStream(in);

			byte[] a = new byte[4000];
			while(b.read(a) != -1){
				System.out.println(a);
				;
			}
			
			done = true;
			
			textArea.append(b.toString());

			Color color = submitButton.getBackground();
			if (canSuspend && filterBox.isSelected()) {
				synchronized (this) {
					submitButton.setBackground(Color.RED);
					this.wait();
				}
			}
			// No matter how we may (or may not) format it, send it
			// on unformatted - we don't want to mess with how its
			// sent to the other side, just how its displayed
			if ((out != null)) {

				out.write(a);
			}
			submitButton.setBackground(color);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			done = true;
			try {
				if (out != null) {
					out.flush();
					if (null != outSocket) {
						outSocket.shutdownOutput();
					} else {
						out.close();
					}
					out = null;
				}
			} catch (Exception e) {
			}
			try {
				if (in != null) {
					if (inSocket != null) {
						inSocket.shutdownInput();
					} else {
						in.close();
					}
					in = null;
				}
			} catch (Exception e) {
			}
			myConnection.wakeUp();
		}
	}

	/**
	 * Method halt
	 */
	public void halt() {
		try {
			if (inSocket != null) {
				inSocket.close();
			}
			if (outSocket != null) {
				outSocket.close();
			}
			inSocket = null;
			outSocket = null;
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
			in = null;
			out = null;
			done = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
